//
//  AddGoods.m
//  reSearchDemo
//
//  Created by 科技 on 15/8/19.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "AddGoods.h"
#import "CommoditySearch.h"
#import "AddGoodsCell.h"
@interface AddGoods () <UITableViewDataSource,UITableViewDelegate>{
    UITableView *_tableView;
    NSMutableArray *_dataSource;
}

@end

@implementation AddGoods

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"商品列表";
    UIBarButtonItem *bar1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(SearchBtnAction:)];
    UIBarButtonItem *bar2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(AddBtnAction:)];
    NSArray *arr = @[bar2,bar1];
    self.navigationItem.rightBarButtonItems = arr;
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(SearchBtnAction:)];
    [self reloadDataSource];
    [self allocTableView];
}
#pragma mark --加载数据--
- (void)reloadDataSource {
    _dataSource = [NSMutableArray array];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"鞋子" forKey:@"name"];
    [dic setObject:@"服装" forKey:@"category"];
    [dic setObject:@"99.9" forKey:@"price"];
    [dic setObject:@"是" forKey:@"shelves"];
    [dic setObject:@"0.0" forKey:@"height"];
    
    NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
    [dic1 setObject:@"袜子" forKey:@"name"];
    [dic1 setObject:@"服装" forKey:@"category"];
    [dic1 setObject:@"19.9" forKey:@"price"];
    [dic1 setObject:@"否" forKey:@"shelves"];
    [dic1 setObject:@"0.0" forKey:@"height"];
    
    NSMutableDictionary *dic2 = [NSMutableDictionary dictionary];
    [dic2 setObject:@"衣服" forKey:@"name"];
    [dic2 setObject:@"服装" forKey:@"category"];
    [dic2 setObject:@"119.9" forKey:@"price"];
    [dic2 setObject:@"是" forKey:@"shelves"];
    [dic2 setObject:@"0.0" forKey:@"height"];
    
    [_dataSource addObject:dic];
    [_dataSource addObject:dic1];
    [_dataSource addObject:dic2];
}
#pragma mark --初始化tableView--
- (void)allocTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    
    [self.view addSubview:_tableView];
}
#pragma mark --设置tableView头视图--
- (UIView *)AllocTableHeadView {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 20)];
    headView.backgroundColor = [UIColor colorWithRed:247/255.0 green:247/255.0 blue:247/255.0 alpha:0.9];
    UILabel *label;
    NSArray *titleArr = @[@"名称",@"分类",@"价格",@"上架"];
    for (int i = 0; i<4; i++) {
        label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)/4*i, 0, CGRectGetWidth(self.view.frame)/4, CGRectGetHeight(headView.frame))];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = titleArr[i];
        [headView addSubview:label];
        label = nil;
    }
    
    return headView;
}

#pragma mark --搜索商品--
- (void)SearchBtnAction:(UIBarButtonItem *)sender {
    [self pushViewController:[CommoditySearch new]];
}
#pragma mark --添加商品--
- (void)AddBtnAction:(UIBarButtonItem *)sender {
    
}

#pragma mark -
#pragma mark --tableViewDelegate --
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *indentifier = @"cell";
    AddGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[AddGoodsCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indentifier];
    }
    cell.model = _dataSource[indexPath.row];

    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    
    return [self AllocTableHeadView];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
